/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.misc;

import java.sql.*;
import java.io.*;

import nfse.NetFSE;

public class MetadataPathFixer {

    // Usage: java MetadataPathFixer dstip|srcip|dstport
    public static void main(String[] args) {
        try {
            if (args.length == 0) {
                System.err.println("Usage: java MetadataPathFixer correct_root");
                return;
            }
            String sql = "select * from Static_Indices";
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection connection = DriverManager.getConnection("jdbc:mysql://" + NetFSE.METADATA_HOST + "/"
                    + NetFSE.METADATA_DATABASE, NetFSE.METADATA_USER, NetFSE.METADATA_PASSWORD);
            Statement statement = connection.createStatement();
            Statement statement2 = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                String path = rs.getString("DB_Path");
                File pathFile = new File(path);
                if (!pathFile.exists()) {
                    String newPath = args[0] + path.substring(path.indexOf('/', 1));
                    int month = rs.getInt("Month");
                    int day = rs.getInt("Day");
                    int year = rs.getInt("Year");
                    int run = rs.getInt("Run");
                    String date = rs.getString("Index_Date");
                    int field = rs.getInt("Field_ID");
                    int sensor = rs.getInt("Sensor_ID");
                    long min = rs.getLong("Min_Time");
                    long max = rs.getLong("Max_Time");
                    sql = "update Static_Indices set DB_Path='" + newPath + "' where Month=" + month + " and Day="
                            + day + " and Year=" + year + " and Run=" + run + " and Field_ID=" + field
                            + " and Sensor_ID=" + sensor;
                    statement2.execute(sql);
                    // System.out.println(sql);
                }
            }
            rs.close();
            statement2.close();
            statement.close();
            connection.close();
        } catch (Exception E) {
            E.printStackTrace();
        }
    }

}
